//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using System.Collections.Generic;

namespace ViNoToolkit{

	/// <summary>
	/// Vi no scene save util.
	/// </summary>
	public class ViNoSceneSaveUtil {

		/// <summary>
		/// Loads the scene from XML text asset.
		/// </summary>
		/// <param name="txt">TextAsset</param>
		static public void LoadSceneXMLFromTextAsset( TextAsset txt ){
			ViNoGameSaveLoad.LoadSceneFromXmlString(  txt.text );		
		}
		
		/// <summary>
				/// Loads the scene from XML TextAsset path.
		/// </summary>
		/// <param name="path">Path.</param>
		static public void LoadSceneXMLFromTextAssetPath( string path ){
			TextAsset txt = Resources.Load( path ,  typeof( TextAsset ) ) as TextAsset;
			if( txt != null ){		
				LoadSceneXMLFromTextAsset( txt );			
			}
			else{
				ViNoDebugger.LogError( "SaveInfo" , "LOAD_SCENE_XML : Resources.Load Error! " );	
			}					
		}
		
		/// <summary>
		/// Saves the data recursively.
		/// </summary>
		/// <param name="savedObj">Saved object.</param>
		/// <param name="nodelist">Nodelist.</param>
		/// <param name="spriteFact">Sprite fact.</param>
		static public void SaveDataRecursively( GameObject savedObj , ref List<SceneData.SceneNodeData> nodelist , ref ISpriteFactory spriteFact ){
			SceneData.SceneNodeData data = new SceneData.SceneNodeData();
			nodelist.Add( data );
			spriteFact.SaveData( ref data , savedObj );					
			
			int count = savedObj.transform.GetChildCount();
			if( count > 0 ){
				for(int i=0;i<count;i++){		
					GameObject childObj =  savedObj.transform.GetChild( i ).gameObject;
					SaveDataRecursively( childObj ,ref nodelist , ref spriteFact );
				}									
			}		
		}
		
		/// <summary>
		/// Saves the panel children.
		/// </summary>
		/// <param name="savedObj">Saved object.</param>
		/// <param name="nodelist">Nodelist.</param>
		/// <param name="spriteFact">Sprite fact.</param>
		static public void SavePanelChildren( GameObject savedObj , ref List<SceneData.SceneNodeData> nodelist , ref ISpriteFactory spriteFact ){
			nodelist.Clear();				
			
			// savedObj <== named "SavedLayer" GameObject and this is not serialized.
			int count = savedObj.transform.GetChildCount();
			if( count > 0 ){
				for(int i=0;i<count;i++){		
					GameObject childObj =  savedObj.transform.GetChild( i ).gameObject;
					SaveDataRecursively( childObj ,ref nodelist , ref spriteFact );
				}									
			}					
		}

		/// <summary>
		/// Saves the vi no scene nodes.
		/// </summary>
		/// <param name="nodelist">Nodelist.</param>
		/// <param name="fact">Fact.</param>
		static public void SaveViNoSceneNodes(  ref List<SceneData.SceneNodeData> nodelist , ISpriteFactory fact ){
			ViNoSceneNode[] sceneNodesData = GameObject.FindObjectsOfType( typeof( ViNoSceneNode ) ) as ViNoSceneNode[];
			if( sceneNodesData != null ){
				for( int i=0;i<sceneNodesData.Length;i++){
					GameObject sceneNode = sceneNodesData[ i ].gameObject;			
					if( sceneNodesData[ i ].singleNodeData != null ){
						SceneData.SceneNodeData data =  sceneNodesData[ i ].singleNodeData;									
						nodelist.Add( data );				
						fact.SaveData( ref data , sceneNode );					
					}
				}
			}		
		}
							
	}
}
